package labox.innovation.gameserver.network.clientpackets;

import javolution.util.FastList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.ThreadPoolManager;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.base.ClassId;
import labox.innovation.gameserver.network.serverpackets.GUIList;
import labox.innovation.gameserver.rank.RankManager;
import labox.innovation.gameserver.rank.RankPlayerInfo;

/**
 * 接收客户端请求排行榜信息包
 * @author Frank
 *
 */
public final class RankRequest extends L2GameClientPacket {

	private static final String _C__37_RANKREQUEST = "[C] 37 RankRequest";

	private static final Logger _log = LoggerFactory.getLogger(Logout.class.getName());

	/**当前玩家排行*/
	private static final byte RANK_TYPE_SELF = 0;

	/**全服等级排行*/
	private static final byte RANK_TYPE_LEVEL = 1;

	/**全服技能排行*/
	private static final byte RANK_TYPE_SKILL = 2;

	/**全服财富排行*/
	private static final byte RANK_TYPE_WEALTH = 3;

	/**金刚等级排行*/
	private static final byte RANK_TYPE_KINGKONG_LEVEL = 4;

	/**御剑等级排行*/
	private static final byte RANK_TYPE_SWORDER_LEVEL = 5;

	/**降雷等级排行*/
	private static final byte RANK_TYPE_MINER_LEVEL = 6;

	/**罗刹等级排行*/
	private static final byte RANK_TYPE_DEMON_LEVEL = 7;

	/**幽冥等级排行*/
	private static final byte RANK_TYPE_NETHER_LEVEL = 8;

	/**勾魂等级排行*/
	private static final byte RANK_TYPE_BEWITCHER_LEVEL = 9;

	/**金刚技能排行*/
	private static final byte RANK_TYPE_KINGKONG_SKILL = 10;

	/**御剑技能排行*/
	private static final byte RANK_TYPE_SWORDER_SKILL = 11;

	/**降雷技能排行*/
	private static final byte RANK_TYPE_MINER_SKILL = 12;

	/**罗刹技能排行*/
	private static final byte RANK_TYPE_DEMON_SKILL = 13;

	/**幽冥技能排行*/
	private static final byte RANK_TYPE_NETHER_SKILL = 14;

	/**勾魂技能排行*/
	private static final byte RANK_TYPE_BEWITCHER_SKILL = 15;

	/**金刚财富排行*/
	private static final byte RANK_TYPE_KINGKONG_WEALTH = 16;

	/**御剑财富排行*/
	private static final byte RANK_TYPE_SWORDER_WEALTH = 17;

	/**降雷财富排行*/
	private static final byte RANK_TYPE_MINER_WEALTH = 18;

	/**罗刹财富排行*/
	private static final byte RANK_TYPE_DEMON_WEALTH = 19;

	/**幽冥财富排行*/
	private static final byte RANK_TYPE_NETHER_WEALTH = 20;

	/**勾魂财富排行*/
	private static final byte RANK_TYPE_BEWITCHER_WEALTH = 21;

	/**玩家好友等级排行*/
	private static final byte RANK_TYPE_FRIEND_LEVEL = 22;

	/**玩家好友技能排行*/
	private static final byte RANK_TYPE_FRIEND_SKILL = 23;

	/**玩家好友财富排行*/
	private static final byte RANK_TYPE_FRIEND_WEALTH = 24;
	
	private final String[] titles = {"当前玩家排行", "全服等级排行", "全服技能排行", "全服财富排行", "金刚等级排行", "御剑等级排行", "降雷等级排行", "罗刹等级排行", "幽冥等级排行", "勾魂等级排行", "金刚技能排行", "御剑技能排行", "降雷技能排行", "罗刹技能排行", "幽冥技能排行" ,"勾魂技能排行", "金刚财富排行", "御剑财富排行", "降雷财富排行", "罗刹财富排行", "幽冥财富排行", "勾魂财富排行", "玩家好友等级排行", "玩家好友技能排行", "玩家好友财富排行"};

	private byte rankType;

	@Override
	protected void readImpl() {
		rankType = readByte();
	}

	@Override
	protected void runImpl() {
		ThreadPoolManager.getInstance().execute(new Runnable() {
			public void run() {
				FuzePcInstance activeChar = getClient().getActiveChar();
				if (activeChar == null)
					return;
				
				StringBuilder sb = new StringBuilder();
				RankPlayerInfo playerinfo = null;
				FastList<RankPlayerInfo> list = null;
				
				ClassId classId = null;
				
				switch (rankType) {
				case RANK_TYPE_SELF:
					break;
				case RANK_TYPE_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfLevel();
					break;
				case RANK_TYPE_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfSkill();
					break;
				case RANK_TYPE_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfWealth();
					break;
				case RANK_TYPE_KINGKONG_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfKingKongLevel();
					classId = ClassId.fighter;
					break;
				case RANK_TYPE_SWORDER_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfSworderLevel();
					classId = ClassId.taoist;
					break;
				case RANK_TYPE_MINER_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfMinerLevel();
					classId = ClassId.mage;
					break;
				case RANK_TYPE_DEMON_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfDemonLevel();
					classId = ClassId.elffighter;
					break;
				case RANK_TYPE_NETHER_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfNetherLevel();
					classId = ClassId.elftaoist;
					break;
				case RANK_TYPE_BEWITCHER_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfBewitcherLevel();
					classId = ClassId.elfmage;
					break;
				case RANK_TYPE_KINGKONG_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfKingKongSkill();
					classId = ClassId.fighter;
					break;
				case RANK_TYPE_SWORDER_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfSworderSkill();
					classId = ClassId.taoist;
					break;
				case RANK_TYPE_MINER_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfMinerSkill();
					classId = ClassId.mage;
					break;
				case RANK_TYPE_DEMON_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfDemonSkill();
					classId = ClassId.elffighter;
					break;
				case RANK_TYPE_NETHER_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfNetherSkill();
					classId = ClassId.elftaoist;
					break;
				case RANK_TYPE_BEWITCHER_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfBewitcherSkill();
					classId = ClassId.elfmage;
					break;
				case RANK_TYPE_KINGKONG_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfKingKongWealth();
					classId = ClassId.fighter;
					break;
				case RANK_TYPE_SWORDER_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfSworderWealth();
					classId = ClassId.taoist;
					break;
				case RANK_TYPE_MINER_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfMinerWealth();
					classId = ClassId.mage;
					break;
				case RANK_TYPE_DEMON_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfDemonWealth();
					classId = ClassId.elffighter;
					break;
				case RANK_TYPE_NETHER_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfNetherWealth();
					classId = ClassId.elftaoist;
					break;
				case RANK_TYPE_BEWITCHER_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenOfBewitcherWealth();
					classId = ClassId.elfmage;
					break;
				case RANK_TYPE_FRIEND_LEVEL:
					playerinfo = RankManager.getInstance().getLevelRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenLevelOfFriends(activeChar.getObjectId());
					break;
				case RANK_TYPE_FRIEND_SKILL:
					playerinfo = RankManager.getInstance().getSkillRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenSkillOfFriends(activeChar.getObjectId());
					break;
				case RANK_TYPE_FRIEND_WEALTH:
					playerinfo = RankManager.getInstance().getWealthRankByPlayerID(activeChar.getObjectId());
					list = RankManager.getInstance().getTopTenWealthOfFriends(activeChar.getObjectId());
					break;
				default:
					_log.warn("No correct request!", Config.SERVER_ID, "排行榜");
					return;
				}

				sb.append("<form nm='sontop' tl='").append(titles[rankType]).append("'>\n");
				sb.append("	<!--排行榜的最终界面-->\n");
				sb.append("	<tb ln='n' h='90%' bt='1'>\n");
				sb.append("		<tb ln='l' w='90%'>\n");
				sb.append("		<tr h='5'><td></td></tr>\n");
				
				if (list != null && list.size() != 0) {
					String temp;
					
					for (int i = 0; i < list.size(); i++) {
//						temp = "	    <tr><td><a nm='" + i + "' pl='min' cmd='' oc='click()'  val='" + list.get(i).getCharid() + "'>" + list.get(i).getCharName() + "</a></td><td><a nm='l0' pl='min' >LV" + list.get(i).getLevel() + "</a></td></tr>\n";
//						sb.append(temp);
						sb.append("	    <tr><td><a nm='").append(i).append("' pl='min' cmd='' oc='click()'  val='").append(list.get(i).getCharid()).append("'>").append(list.get(i).getCharName()).append("</a></td><td><a nm='l0' pl='min' >");
						if (rankType == RANK_TYPE_LEVEL || rankType == RANK_TYPE_FRIEND_LEVEL || ((rankType >= RANK_TYPE_KINGKONG_LEVEL) && (rankType <= RANK_TYPE_BEWITCHER_LEVEL))) {
							sb.append("LV").append(list.get(i).getLevel()).append("</a></td></tr>\n");
						} else {
							sb.append(list.get(i).getLevel()).append("</a></td></tr>\n");
						}
					}
				}
				
				String selfRank = "新用户未参与排名！";
				if (playerinfo != null) {
					selfRank = "第" + String.valueOf(playerinfo.getRank()) + "名";
				}
				sb.append("		</tb>\n");
				sb.append("	</tb>");
				sb.append("	<tb ln='n'>\n");
				if (classId != null && classId != getClient().getActiveChar().getClassId()) {
					
				} else {
					sb.append("	 <tr><td><a pl='r' >个人排名：</a></td><td><a nm='me' pl='l'>").append(selfRank).append("</a></td></tr>\n");
				}
//				sb.append("	 <tr><td><a pl='min' >个人排名：</a></td><td><a nm='me' pl='min'>").append(selfRank).append("</a></td></tr>\n");
				sb.append("	</tb>\n");
				sb.append("	<form nm='topmenu' w='65' h='86' pl='fw'>\n");
				sb.append("		    <tb ln='l'>\n");
				sb.append("		    <tr><td><a nm='look' cmd='' oc='back()' hk='1'>查看</a></td></tr>\n");
				sb.append("		    <tr><td><a nm='talk' cmd='' oc='' hk='2'>密聊</a></td></tr>\n");
				sb.append("		    <tr><td><a nm='firend' cmd='' oc='back()' hk='3'>交友</a></td></tr>\n");
				sb.append("		    <tr><td><a cmd='' hk='4' oc='back()'>关闭</a></td></tr>\n");
				sb.append("		    </tb>\n");
				sb.append("	</form>\n");
				sb.append("	<s>\n");
				sb.append("	sub click()\n");
				sb.append("	popForm(\"sontop\",\"topmenu\")\n");
				sb.append("	val=getVal(getFocusName(\"sontop\"))\n");
				sb.append("	name=getText(getFocusName(\"sontop\"))\n");
				sb.append("	setCmd(\"topmenu.look\",\"send:34:l\"+val)\n");
				sb.append(" 	setCmd(\"topmenu.talk\",\"g:chat:\"+val+\":\"+name)\n");
				sb.append(" 	setCmd(\"topmenu.firend\",\"send:161:l\"+val)\n");
				sb.append("	ret\n");
				sb.append("	</s>\n");
				sb.append("</form>\n");
				
				getClient().getActiveChar().sendPacket(new GUIList(sb.toString()));
			}
		});
	}

	@Override
	public String getType() {
		return _C__37_RANKREQUEST;
	}
}
